package com.linghang.wusthelper;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.HashMap;
import java.util.Map;

public class Login implements PageProcessor {

    private Site site = Site.me()
            .setRetryTimes(3)
            .setSleepTime(1000)
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
            .addHeader("Connection", "keep-alive")
            .addHeader("X-Requested-With", "XMLHttpRequest")
            .addHeader("Content-Type",
                    "application/x-www-form-urlencoded;charset=utf-8")
            .addHeader(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");


    @Override
    public void process(Page page) {
        System.out.println(page.getHtml());
    }

    @Override
    public Site getSite() {
        site.addCookie("PHPSESSID","1");
        Request request = new Request("http://www.lib.wust.edu.cn:8780/reader/redr_verify.php");
        Map<String, Object> map = new HashMap<>();
        map.put("number","201613136008");
        map.put("password","141317");
        map.put("select","bar_no");
        map.put("returnUrl","");
        map.put("csrf_token","La:]bF8s,I");
        request.setMethod(HttpConstant.Method.POST);
        request.setExtras(map);
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new Login())
                .addUrl("http://www.lib.wust.edu.cn:8780/reader/redr_verify.php")
                .run();
    }
}
